<template>
    <div>
        <el-card>
            <el-row :gutter="12">
                <el-col :span="6">
                    <el-input
                        placeholder="请输入供应商名称"
                        class="input-with-select"
                        v-model="queryInfo.vendorName"
                        clearable
                        @clear="getVendorList"
                    >
                    </el-input>
                </el-col>

                <el-col :span="2">
                    <el-button type="primary" @click="getVendorList"
                        >搜索</el-button
                    >
                </el-col>
                <el-col :span="2">
                    <el-button type="primary" @click="newVendor"
                        >添加供应商</el-button
                    >
                </el-col>
            </el-row>
            <el-table :data="vendorList" border stripe>

                <el-table-column label="id" prop="id"></el-table-column>
                <el-table-column label="供应商名称" prop="vendorName"></el-table-column>
                <el-table-column label="联系人" prop="contactPerson"></el-table-column>
                <el-table-column label="手机号" prop="mobile"></el-table-column>
                <el-table-column label="联系电话" prop="telephone"></el-table-column>
                <el-table-column label="地址" prop="address"></el-table-column>
                <el-table-column label="添加时间" prop="addTime"></el-table-column>

                <el-table-column label="编辑">
                    <template slot-scope="scope">
                        <el-button
                            type="primary"
                            icon="el-icon-edit"
                            size="mini"
                            @click="editVendor(scope.row)"
                        ></el-button>
                    </template>
                </el-table-column>
                <!-- 分页 -->
            </el-table>
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum"
                :page-sizes="[5, 10, 15, 20]"
                :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
            >
            </el-pagination>
        </el-card>
        <el-dialog title="修改供应商" :visible.sync="addVendorVisible" width="60%">
            <el-form :model="addVendorForm" ref="addVendorRef" :rules="addVendorRules" label-width="150px">
                <el-form-item label="供应商名称" prop="vendorName">
                    <el-input
                        v-model="addVendorForm.vendorName"
                        class="form_ipt"
                    ></el-input>
                </el-form-item>
                 <el-form-item label="联系人" prop="contactPerson">
                    <el-input
                        v-model="addVendorForm.contactPerson"
                        class="form_ipt"
                    ></el-input>
                </el-form-item>
                <el-form-item label="手机号" prop="mobile">
                    <el-input
                        v-model="addVendorForm.mobile"
                        class="form_ipt"
                    ></el-input>
                </el-form-item>
                <el-form-item label="联系电话" prop="telephone">
                    <el-input
                        v-model="addVendorForm.telephone"
                        class="form_ipt"
                    ></el-input>
                </el-form-item>
                <el-form-item label="地址" prop="address">
                    <el-input
                        v-model="addVendorForm.address"
                        class="form_ipt"
                    ></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addVendorVisible = false">取 消</el-button>
                <el-button type="primary" @click="addVendor">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import MallApi from "@/api/Mall.js";
export default {
    data() {
        return {
            queryInfo: {
                vendorName: "",
                pagenum: 1, // 页码
                pagesize: 10, // 页大小
            },
            vendorList: [],
            total: 0,
            addVendorVisible: false,
            addVendorForm: {
                id: "",
                vendorName: "",
                vendorCode: "",
                mobile: "",
                telephone: "",
                address: "",
                contactPerson: ""
            },
            addVendorRules: {
                vendorName: [
                    {
                        required: true,
                        message: "请输入供应商名字",
                        trigger: "change",
                    }
                ]
            }
        };
    },
    watch: {

    },
    created() {
        this.getVendorList();
    },
    methods: {
        getVendorList() {
          MallApi.getVendorList(this.queryInfo).then((res) => {
                if (res.success) {
                    this.vendorList = res.data.vendorList;
                    this.total = res.data.page.total;
                } else {
                    return this.$message.error("获取供应商列表失败");
                }
            });
        },
        handleSizeChange(pagesize) {
            this.queryInfo.pagenum = 1;
            this.queryInfo.pagesize = pagesize;
            this.getVendorList();
        },
        // 监听pagenum的事件
        handleCurrentChange(pagenum) {
            this.queryInfo.pagenum = pagenum;
            this.getVendorList();
        },
        newVendor() {
            this.addVendorVisible = true;
            this.addVendorForm = {
               id: "",
                vendorName: "",
                vendorCode: "",
                mobile: "",
                contactPerson: "",
                telephone: "",
                address: ""
            };
        },
        editVendor(info) {
          MallApi.getVendorDetail({
                id: info.id,
            }).then((res) => {
                if (res.success) {
                    this.addVendorVisible = true;
                    this.addVendorForm = JSON.parse(JSON.stringify(res.data));
                }
            });
        },

        // 添加
        addVendor() {
            this.$refs.addVendorRef.validate((valid) => {
                if (valid) {
                  MallApi.saveVendor(this.addVendorForm).then((res) => {
                        if (res.success) {
                            this.addVendorVisible = false;
                            this.getVendorList();
                        }
                    });
                } else {
                    console.log("error submit!!");
                    return false;
                }
            });
        }
    }
};
</script>

<style>
.form_ipt {
    width: 450px;
}
</style>
